document.addEventListener('DOMContentLoaded', function() {
    const carousel = document.getElementById('carousel');
    const slides = document.querySelectorAll('.carousel-item');
    const indicators = document.querySelectorAll('.carousel_btnDiv_btn');
    const prevBtn = document.getElementById('direction_left');
    const nextBtn = document.getElementById('direction_right');
    const carouselImages = document.querySelectorAll('.carousel-item img');
    
    let currentIndex = 0;
    let slideInterval;
    const slideCount = slides.length;
    
    $("#note_right a").click(function(e){
    	e.preventDefault();
    	videoJumpTo($(this).find("input").val())
    })
    
    $(".carousel-item").click(function(){
    	videoJumpTo($(this).find("input").val())
    })
   
    $("#note_bottom").on("click",".note_link",function(e){
    	e.preventDefault();
    	videoJumpTo($(this).find("input").val())
    })
   
	function videoJumpTo(videoId) {
	  $.ajax({
	    url: "selectVideo.do",
	    type: "GET",
	    data: { 
	    	action:"send",
	    	videoId: videoId 
	    	},
	    success: function() {
	        localStorage.setItem('pendingVideoId', videoId);
	        window.open("video.html", "_blank");
	        addHistory(videoId)
	    }
	  });
	}
    
    function addHistory(videoId){
		$.ajax({
			url:"addHistory.do",
			type:"post",
			data:{
				videoId:videoId
			}
		})
	}
    
    // 图片标题数组（可根据需要修改）
    const captions = ['图片标题 1', '图片标题 2', '图片标题 3', '图片标题 4', '图片标题 5', '图片标题 6', '图片标题 7', '图片标题 8', '图片标题 9'];
    
    // 显示指定索引的幻灯片
    function showSlide(index) {
        // 防止索引越界
        if (index < 0) index = slideCount - 1;
        if (index >= slideCount) index = 0;
        
        // 隐藏所有幻灯片并移除指示器活动状态
        slides.forEach((slide, i) => {
            slide.classList.toggle('active', i === index);
            indicators[i].classList.toggle('active', i === index);
        });
        
        // 更新当前索引
        currentIndex = index;
    }
    
    // 下一张幻灯片
    function nextSlide() {
        showSlide(currentIndex + 1);
    }
    
    // 上一张幻灯片
    function prevSlide() {
        showSlide(currentIndex - 1);
    }
    
    // 清除现有计时器并启动新的计时器
    function resetInterval() {
        clearInterval(slideInterval);
        slideInterval = setInterval(nextSlide, 5000); // 延长轮播时间到5秒
    }
    
    // 初始化
    function initCarousel() {
        // 点击指示器切换幻灯片
        indicators.forEach((indicator, i) => {
            indicator.addEventListener('click', () => {
                if (i !== currentIndex) {
                    showSlide(i);
                    resetInterval();
                }
            });
        });
        
        // 左右箭头点击事件
        prevBtn.addEventListener('click', () => {
            prevSlide();
            resetInterval();
        });
        
        nextBtn.addEventListener('click', () => {
            nextSlide();
            resetInterval();
        });
        
        // 鼠标悬停暂停轮播，离开恢复
        carousel.addEventListener('mouseenter', () => {
            clearInterval(slideInterval);
        });
        
        carousel.addEventListener('mouseleave', resetInterval);
        
        // 为每个图片添加鼠标悬停事件
        carouselImages.forEach(img => {
            img.addEventListener('mouseenter', () => {
                clearInterval(slideInterval);
            });
            
            img.addEventListener('mouseleave', resetInterval);
        });
        
        // 初始化自动轮播
        resetInterval();
        
        // 窗口卸载时清除计时器
        window.addEventListener('unload', () => {
            clearInterval(slideInterval);
        });
    }
    
    // 检查DOM元素是否存在再初始化
    if (carousel && slides.length && indicators.length && prevBtn && nextBtn) {
        initCarousel();
    }

    let navigation_right_a = document.querySelectorAll('.navigation_right_a');
    let info = document.getElementById("info")
    for (let i = 0;i<navigation_right_a.length;i++){
        navigation_right_a[i].onclick = function(e){
            e.preventDefault()
            info.innerText = this.innerText.replace(/\s+/g, '')//得到文字并去空格
        }
    }
});



